<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @addTask="addTaskFn"
    :list="list"
    ></TodoHeader>

    <TodoMain :list="showList"
    
    @delTask="delTaskFn"
    ></TodoMain>

    <TodoFooter 
      :type="isShow"
      @changeShow="changeShowFn"
      @cleartask="cleartaskFn"
      :length="showList.length"
    ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"
    
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";


export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem('todoList')) || [],
      isShow : 'all', //all表示显示所有的数组，yes表示已完成的数据，no表示未完成的数据
      
    };
  },
  computed:{
    //showList 存放筛选的数组
    showList(){
      if(this.isShow === 'yes'){
        return this.list.filter(item => item.isDone === true)
      }else if(this.isShow === 'no'){
        return this.list.filter(item => item.isDone === false)
      }else{
        return this.list
      }
    }
  },
  methods:{
    addTaskFn(task){
      this.list.push({
        id: this.list.length ? this.list.at(-1).id + 1 : 100,
        name : task,
        isDone: false,
      })
    },
    delTaskFn(id){
      this.list = this.list.filter(item => item.id !== id)
    },
    changeShowFn(type){
      this.isShow = type
    },
    cleartaskFn(ftype){
      this.isShow = ftype
    }
  },
  // 监听数组，本地存取数据
  watch:{
    list:{
      deep:true,
      handler(){
        localStorage.setItem('todoList', JSON.stringify(this.list))
      }
    }
  }
};
</script>